﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibBinary.FileData
{
    public class PrimitiveData<T> : SystemPrimitiveData
    {
        private T _Data;
        public T Data
        {
            get
            {
                return _Data;
            }
            set
            {
                _Data = value;
                try
                {
                    base.IntData = Convert.ToInt32((object)value);
                }
                catch
                {
                    base.IntData = null;
                }
                try
                {
                    base.BoolData = Convert.ToBoolean((object)value);
                }
                catch
                {
                    base.BoolData = null;
                }
            }
        }

        public PrimitiveData(AbstractFileData parent)
            : base(parent)
        {
        }

        public override string ToString()
        {
            return _Data.ToString();
        }
    }

    public class SystemPrimitiveData : AbstractFileData
    {
        public int? IntData { get; protected set; }
        public bool? BoolData { get; protected set; }

        public SystemPrimitiveData(AbstractFileData parent)
            : base(parent)
        {
        }

        public SystemPrimitiveData(AbstractFileData parent, int value)
            : base(parent)
        {
            SetValue(value);
        }

        public SystemPrimitiveData(AbstractFileData parent, bool value)
            : base(parent)
        {
            SetValue(value);
        }

        public void SetValue(int value)
        {
            IntData = value;
            BoolData = value != 0;
        }

        public void SetValue(bool value)
        {
            IntData = value ? 1 : 0;
            BoolData = value;
        }

        public override AbstractFileData GetChild(int index)
        {
            throw new ReadingFormatException("GetChild in PrimitiveData.");
        }

        public override string ToString()
        {
            return IntData.ToString();
        }
    }
}
